﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;

namespace AssemblyInfoEditor.Project
{
    public class ProjectManager
    {
        private DTE ide;
        private SolutionEvents ideSolutionEvents;
        private ProjectAssemblyInfoCollection asmInfoCollection;
        private bool isReadOnly;

        public ProjectManager(DTE ideApplicationObject)
        {
            isReadOnly = false;

            ide = ideApplicationObject;
            ideSolutionEvents = ide.Events.SolutionEvents;

            ide.Events.BuildEvents.OnBuildBegin += new _dispBuildEvents_OnBuildBeginEventHandler(OnBuildBegin);
            ideSolutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(ideSolutionEvents_Opened);
            ideSolutionEvents.BeforeClosing += new _dispSolutionEvents_BeforeClosingEventHandler(ideSolutionEvents_BeforeClosing);

            asmInfoCollection = new ProjectAssemblyInfoCollection();
        }

        void ideSolutionEvents_BeforeClosing()
        {
            SaveAllProjectAssemblyInfo();
        }

        void ideSolutionEvents_Opened()
        {
            LoadProjectInfo();
        }

        protected virtual void OnBuildBegin(vsBuildScope Scope, vsBuildAction Action)
        {
            // Attempt to call SaveSettings on all ProjectAssemblyInfo objects
            SaveAllProjectAssemblyInfo();

            // Increment project versions for those projects that have enabled it
            if ((Action == vsBuildAction.vsBuildActionBuild) || (Action == vsBuildAction.vsBuildActionRebuildAll))
            {
                foreach (ProjectAssemblyInfo pas in asmInfoCollection)
                {
                    pas.Version.Increment(DateTime.Now);
                    pas.SetProjectProperty("AssemblyVersion", pas.Version.ToString());
                    pas.SetProjectProperty("AssemblyFileVersion", pas.Version.ToString());
                }
            }
        }

        public ProjectAssemblyInfoCollection AssemblyInfoCollection
        {
            get { return asmInfoCollection; }
            set { asmInfoCollection = value; }
        }

        public void LoadProjectInfo()
        {
            if (ide.Solution.IsOpen)
            {
                foreach (EnvDTE.Project prj in ide.Solution.Projects)
                {
                    // Only add the project if it's a CSharp or Visual Basic project
                    if ((prj.Kind == EnvDTEProjectType.prjKindCSharpProject) ||
                        (prj.Kind == EnvDTEProjectType.prjKindVBProject))
                    {
                        AssemblyInfoCollection.Add(new ProjectAssemblyInfo(prj));
                    }
                }
            }
        }

        public void SaveAllProjectAssemblyInfo()
        {
            foreach (ProjectAssemblyInfo pai in asmInfoCollection)
            {
                pai.SaveSettings();
            }
        }
    }
}
